
# 自定义认证模块
from django.contrib.auth.backends import BaseBackend
from django.db.models import Q
from django.http import JsonResponse
from rest_framework_simplejwt import serializers
from rest_framework import status

from Admins.models import *


# 自定义用户模型类

class MyBackend(BaseBackend):
    # 支持使用用户名或者邮箱进行登录
    def authenticate(self, request, username=None, password=None, **kwargs):
        try:
            user = Administrators.objects.get(Q(username=username) | Q(email=username))
        except:
            raise serializers.ValidationError({'error': "未找到该用户"})
        else:
            if user.check_password(password):
                return user
            else:
                raise serializers.ValidationError({'error':"密码错误！"})
